package com.itheima.netmarket.impl.view;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.itheima.netmarket.GlobalConstants;
import com.itheima.netmarket.impl.view.manager.UIManagerImpl;
import com.itheima.netmarket.utils.PromptManager;
import comitheima.netmarket.R;

/**
 * 支付中心发票处理
 * 
 * @author yangxin
 */
public class InvoiceView extends MainView {

	/**
	 * 顶部返回键
	 */
	private TextView headBackText;
	/**
	 * 顶部保存键
	 */
	private TextView invoiceSaveText;
	/**
	 * 发票抬头
	 */
	private EditText invoiceTitleEdit;
	/**
	 * 发票内容
	 */
	private Spinner invoiceContentSpin;
	/**
	 * spinner数据
	 */
	private String spinnerCon;

	private SpinnerAdapter adapter;
	private final String[] adapterData = { "服装", "生活用品", "电子产品", "体育用品", "杂货用品", "办公用品" };

	public InvoiceView(Context context, Bundle bundle) {
		super(context, bundle);
	}

	public InvoiceView(Context context) {
		super(context);
	}

	@Override
	public View getmView() {
		View view = View.inflate(context, R.layout.invoice_activity, null);
		init(view);
		return view;
	}

	/**
	 * 初始化数据
	 * 
	 * @param view
	 */
	private void init(View view) {
		headBackText = (TextView) view.findViewById(R.id.head_back_text);
		invoiceSaveText = (TextView) view.findViewById(R.id.invoice_save_text);
		invoiceTitleEdit = (EditText) view.findViewById(R.id.invoice_title_edit);
		invoiceContentSpin = (Spinner) view.findViewById(R.id.invoice_content_spin);

		setOnclick();

		adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, adapterData);
		invoiceContentSpin.setAdapter(adapter);
		invoiceContentSpin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				spinnerCon = adapterData[position];
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
	}

	private void setOnclick() {
		headBackText.setOnClickListener(this);
		invoiceSaveText.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		bundle = new Bundle();
		switch (v.getId()) {
		case R.id.head_back_text:
			// 返回键
			if (TextUtils.isEmpty(GlobalConstants.invoiceContent)) {
				PromptManager.showCommomDia(context, "亲，你没有搞定发票哟，是否确定返回");
			} else {
				UIManagerImpl.getInstance().changeView(PayCenterView.class);
			}
			break;
		case R.id.invoice_save_text:
			// 保存键
			String ivContent = invoiceTitleEdit.getText().toString().trim();
			if (TextUtils.isEmpty(ivContent) || TextUtils.isEmpty(spinnerCon))
				PromptManager.showErrorDialog(context, "亲，发票没有搞定哟，请确认");
			else {
				GlobalConstants.invoiceContent = ivContent + "," + spinnerCon;
				PromptManager.showToast(context, "保存成功");
			}
			break;
		}
	}

}
